package com.zshan.clinic.common.util.file;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;


/**
 * 文件工具类
 */
@Slf4j
public class FileUtils {


    public static String toBase64DataUrl(BufferedImage image) {
        try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) {
            ImageIO.write(image, "jpg", os);
            String base64 = cn.hutool.core.codec.Base64.encode(os.toByteArray());
            return "data:image/jpeg;base64," + base64;
        } catch (IOException e) {
            log.error("生成验证码图片失败", e);
        }
        return null;
    }

    /**
     * 判断文件是否为图片
     *
     * @param file
     * @return
     */
    public static boolean isImage(File file) {
        if (file != null && file.exists()) {
            Image img;
            try {
                img = ImageIO.read(new FileInputStream(file));
                return !(img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0);
            } catch (Exception e) {
                return false;
            }
        }
        return false;
    }

    /**
     * 文件转换
     *
     * @param fromFile
     * @param toFile
     * @throws Exception
     */
    public static void multipartFileToFile(MultipartFile fromFile, File toFile) throws Exception {
        OutputStream os = null;
        InputStream ins = null;
        try {
            ins = fromFile.getInputStream();
            os = new FileOutputStream(toFile);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ins != null) {
                try {
                    ins.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }


    /**
     * Base64转文件
     *
     * @param base64
     * @param file
     */
    public static void base64ToFile(String base64, File file) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        try {
            byte[] bytes = Base64.getDecoder().decode(base64);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 文件转换
     *
     * @return
     */
    public static void fileToFile(File fromFile, File toFile) {
        OutputStream os = null;
        InputStream ins = null;
        try {
            ins = new FileInputStream(fromFile);
            os = new FileOutputStream(toFile);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ins != null) {
                try {
                    ins.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }


    /**
     * MultipartFile 转 File
     *
     * @param fromFile
     * @throws Exception
     */
    public static File multipartFileToFile(String fileFolder, MultipartFile fromFile) throws Exception {

        File toFile = null;
        if (fromFile.equals("") || fromFile.getSize() <= 0) {
            toFile = null;
        } else {
            toFile = new File(fileFolder + fromFile.getOriginalFilename());
            inputStreamToFile(fromFile, toFile);
        }
        return toFile;
    }


    //获取流文件
    private static void inputStreamToFile(MultipartFile fromFile, File file) {
        OutputStream os = null;
        InputStream ins = null;
        try {
            ins = fromFile.getInputStream();
            os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ins != null) {
                try {
                    ins.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除本地临时文件
     *
     * @param file
     */
    public static void delteTempFile(File file) {
        if (file != null) {
            File del = new File(file.toURI());
            del.delete();
        }
    }

    /**
     * 生成包含指定内容的文本文件，并返回文件路径
     *
     * @param content   要写入的内容
     * @return 生成的文件路径
     */
    public static String generateFile(String fileName,String content) {
        try {
            // 获取 Windows 用户的桌面目录作为文件存储位置
            File file = new File(fileName);
            // 使用 FileWriter 写入文件
            try (FileWriter writer = new FileWriter(file)) {
                writer.write(content);
                writer.close();;
            }
            return file.getAbsolutePath(); // 返回文件路径
        } catch (Exception e) {
            log.error("生成文件发生异常",e);
        }
        return null;
    }
}
